package com.clear.honghefinance.biz.modules.assist_calculate_summary.handler.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.dto.*;
import com.clear.honghefinance.biz.modules.system.member.domain.vo.AdminDTO;
import com.clear.honghefinance.biz.modules.assist_calculate.enums.AssistCalculateCategoryCodeEnum;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.converter.AssistCalculateDepartmentConvertor;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.entity.AssistCalculateDepartment;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.vo.GetAssistCalculateDepartmentVO;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.domain.vo.ListAssistCalculateBaseVO;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.handler.AssistCalculateHandler;
import com.clear.honghefinance.biz.modules.assist_calculate_summary.service.IAssistCalculateDepartmentService;
import com.clear.honghefinance.common.exception.BizException;
import com.clear.honghefinance.common.service.TokenService;
import com.clear.honghefinance.common.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 客户辅助核算Handler
 */
@Component
@Slf4j
public class AssistCalculateDepartmentHandler implements AssistCalculateHandler {

    @Resource
    private IAssistCalculateDepartmentService assistCalculateDepartmentService;
    @Resource
    private TokenService<AdminDTO> tokenService;

    /**
     * 枚举身份的识别
     *
     * @return
     */
    @Override
    public AssistCalculateCategoryCodeEnum getHandlerType() {
        return AssistCalculateCategoryCodeEnum.DEPARTMENT;
    }

    /**
     * 创建部门辅助核算
     *
     * @param dto
     */
    @Override
    public boolean create(Object dto) {
        AssistCalculateDepartment assistCalculate = AssistCalculateDepartmentConvertor.INSTANCE.convertDTOToEntity((CreateAssistCalculateDepartmentDTO) dto);
        assistCalculate.initDefault();
        assistCalculate.setTenantId(tokenService.getThreadLocalTenantId());
        assistCalculate.setMemberId(tokenService.getThreadLocalUserId());
        assistCalculate.setUpdateMemberId(tokenService.getThreadLocalUserId());

        if (assistCalculateDepartmentService.save(assistCalculate)) {
            return true;
        }
        throw new BizException("创建部门辅助核算失败");
    }

    /**
     * 修改部门辅助核算
     *
     * @param dto
     * @return
     */
    @Override
    public boolean update(Object dto) {
        AssistCalculateDepartment assistCalculate = AssistCalculateDepartmentConvertor.INSTANCE.convertDTOToEntity((UpdateAssistCalculateDepartmentDTO) dto);
        assistCalculate.setUpdateMemberId(tokenService.getThreadLocalUserId());
        assistCalculate.setUpdateTime(DateUtil.getSystemTime());
        LambdaUpdateWrapper<AssistCalculateDepartment> lambdaUpdateWrapper = Wrappers.<AssistCalculateDepartment>lambdaUpdate()
                .eq(AssistCalculateDepartment::getAssistCalculateSummaryId, assistCalculate.getId())
                .eq(AssistCalculateDepartment::getTenantId, assistCalculate.getTenantId())
                .eq(AssistCalculateDepartment::getDelFlag, false);

        if (assistCalculateDepartmentService.update(assistCalculate, lambdaUpdateWrapper)) {
            return true;
        }
        throw new BizException("修改部门辅助核算失败");
    }

    /**
     * 根据辅助核算id查询辅助核算列表
     *
     * @param ids 辅助核算id列表
     * @return
     */
    @Override
    public List<? extends ListAssistCalculateBaseVO> listByAssistCalculateSummaryIds(List<Long> ids) {
        try {
            LambdaQueryWrapper<AssistCalculateDepartment> lambdaQueryWrapper = Wrappers.<AssistCalculateDepartment>lambdaQuery()
                    .eq(AssistCalculateDepartment::getTenantId, tokenService.getThreadLocalTenantId())
                    .eq(AssistCalculateDepartment::getDelFlag, false)
                    .in(CollectionUtils.isNotEmpty(ids), AssistCalculateDepartment::getAssistCalculateSummaryId, ids)
                    .select(AssistCalculateDepartment::getId,
                            AssistCalculateDepartment::getDisable,
                            AssistCalculateDepartment::getAssistCalculateSummaryId,
                            AssistCalculateDepartment::getManager,
                            AssistCalculateDepartment::getPhone,
                            AssistCalculateDepartment::getStartDate,
                            AssistCalculateDepartment::getRevokeDate
                    );
            List<AssistCalculateDepartment> list = assistCalculateDepartmentService.list(lambdaQueryWrapper);

            return AssistCalculateDepartmentConvertor.INSTANCE.convertEntityToVO(list);
        } catch (Exception e) {
            throw new BizException("查询客户辅助核算列表失败");
        }
    }


    /**
     * 根据辅助核算id查询辅助核算详情
     *
     * @param id 辅助核算id
     * @return
     */
    @Override
    public GetAssistCalculateDepartmentVO getDetailById(long id) {
        try {
            LambdaQueryWrapper<AssistCalculateDepartment> lambdaQueryWrapper = Wrappers.<AssistCalculateDepartment>lambdaQuery()
                    .eq(AssistCalculateDepartment::getTenantId, tokenService.getThreadLocalTenantId())
                    .eq(AssistCalculateDepartment::getDelFlag, false)
                    .eq(AssistCalculateDepartment::getAssistCalculateSummaryId, id)
                    .select(AssistCalculateDepartment::getId,
                            AssistCalculateDepartment::getDisable,
                            AssistCalculateDepartment::getAssistCalculateSummaryId,
                            AssistCalculateDepartment::getManager,
                            AssistCalculateDepartment::getPhone,
                            AssistCalculateDepartment::getStartDate,
                            AssistCalculateDepartment::getRevokeDate
                    );
            AssistCalculateDepartment assistCalculate = assistCalculateDepartmentService.getOne(lambdaQueryWrapper);

            return AssistCalculateDepartmentConvertor.INSTANCE.convertEntityToVO(assistCalculate);
        } catch (Exception e) {
            throw new BizException("查询辅助自定义核算详情失败");
        }
    }

}
